package com.cty.servlet;

import com.cty.pojo.User;
import com.cty.service.UserService;
import com.cty.service.impl.UserServiceImpl;
import org.apache.log4j.Logger;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * @Auther: cty
 * @Date: 2020/2/29 20:21
 * @Description: 测试
 * @version: 1.0
 */
@WebServlet(name = "UserServlet", urlPatterns = "/user")
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取日志对象
        Logger log = Logger.getLogger(UserServlet.class);
        // 获取请求参数
        User user = new User();
        user.setUid(0);  // 任意，该值不上传数据库
        user.setUname(req.getParameter("uname"));
        user.setPwd(req.getParameter("pwd"));
        try{
            int sex = Integer.parseInt(req.getParameter("sex"));
            int age = Integer.parseInt(req.getParameter("age"));
        }catch (NumberFormatException e){
            log.error("输入性别或年龄不合法");
            // 重定向到失败页面
            resp.sendRedirect("/opensessioninview/error.jsp");
            return;
        }
        user.setBirth(req.getParameter("birth"));
        // 处理请求参数
        UserService us = new UserServiceImpl();
        int index = us.insService(user);
        if(index > 0){
            // 重定向到成功页面
            resp.sendRedirect("/opensessioninview/success.jsp");
        }else {
            // 重定向到失败页面
            resp.sendRedirect("/opensessioninview/error.jsp");
        }
    }
}
